//////////////////////////////////////////////////
//  File      : dockicon.h                      //
//  Originally written by: g_cigala@virgilio.it //
//  Modified by: ludmiloff@gmail.com            //
//  Copyright : GPL                             //
//////////////////////////////////////////////////

#include "dockicon.h"
#include "adx.h"

extern Adx *deluxe;

DockIcon::DockIcon(Client *c, QWidget *parent) :
    QWidget(parent),client(c), desktopFile(0),
    type(Task), state(Running)

{
	readSettings();
    tipLabel = new TipLabel("test");
}

DockIcon::DockIcon(const QString &file, QWidget *parent) :
    QWidget(parent), client(0), desktopFile(0),
    type(Launcher), state(Stopped)
{
    desktopFile = new AmeDesktopFile(file);
    readSettings();
}

DockIcon::~DockIcon()
{
    if (desktopFile)
        delete desktopFile;
    if (tipLabel)
        delete tipLabel;
}


void DockIcon::readSettings()
{
    // for further use, if needed
}

void DockIcon::showClient()
{
	if (type == DockIcon::Task) {
		client->map();
		close();
	}
}

void DockIcon::updateSize(int w, int h)
{
    setFixedSize(h, h);
}

void DockIcon::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.drawPixmap(QRect(3, 3, height()-6, height()-6), client->appIcon,
        QRect(0, 0, client->appIcon.width(), client->appIcon.height()));// dock icon
   
}

void DockIcon::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton) {
		if (type == DockIcon::Task) {
			emit showIcon(this);
			client->map();
			close();
		}
	}

	if (event->button() == Qt::RightButton) {
		QMenu *menu = new QMenu(this);
		menu->addAction( QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton), "Close");
		menu->popup(event->globalPos());
		connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(runMenu(QAction *)));
	}
}

void DockIcon::enterEvent(QEvent *)
{
    qDebug() << "Enter Icon";
    tipLabel->move(200, 200);
    tipLabel->show();
}

void DockIcon::leaveEvent(QEvent *)
{
    qDebug() << "Leave event";
    tipLabel->hide();
}

void DockIcon::runMenu(QAction *)
{
	emit destroyIcon(this);
	client->destroyClient();
	close();
}
